#define __JO_DBG_

#include <RCSwitch.h>
#include "joystick.h"

#define RC_SWITCH_PIN           10
#define MOD_AUTO                0
#define MOD_TRACK               1
#define MOD_RC433               2
#define MOD_BLUETOOTH           3

#define MASK_JOYSTICK           0xa00000
#define MASK_COMMAND            0xffffff

#define CMD_SET_MOD_AUTO        0xa10000
#define CMD_SET_MOD_TRACK       0xa10001
#define CMD_SET_MOD_RC433       0xa10002
#define CMD_SET_MOD_BLUETOOTH   0xa10003

Joystick *joystick;
RCSwitch sender;

void on_joystick_change (uint32_t value);

void setup () {
    Serial.begin (9600);
    joystick = new Joystick ();
    joystick->attach (A5, A4, NULL);

    sender.setRepeatTransmit (4);
    sender.enableTransmit(RC_SWITCH_PIN);
}

void loop() {
    joystick->check (on_joystick_change);
}

/**
 * send the joystick's value to peer.
 */
void on_joystick_change (uint32_t value) {
    value &= MASK_COMMAND;
    Serial.print ("read value = ");
    Serial.println (value, HEX);
    value |= MASK_JOYSTICK;
    Serial.print ("sending data: ");
    Serial.println (value, HEX);
    long now = millis ();
    sender.send (value, 24);
    now = millis () - now;
    Serial.println (now);
}

void send_button_command (uint32_t cmd) {
    sender.send (cmd, 24);
}

